package com.wbao.dianniu.adapter;

import java.util.List;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wbao.dianniu.R;
import com.wbao.dianniu.indexView.SortModel;
import com.wbao.dianniu.utils.PictureDownloadUtils;
import com.wbao.dianniu.utils.Utils;

/**
 * 我的关注 适配器
 *
 * @author yejianjun
 */
public class FollowAdapter extends BaseAdapter {
    private List<SortModel> list = null;
    private Context mContext;

    public FollowAdapter(Context mContext, List<SortModel> list) {
        this.mContext = mContext;
        this.list = list;
    }

    /**
     * 当ListView数据发生变化时,调用此方法来更新ListView
     *
     * @param list
     */
    public void updateListView(List<SortModel> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (null == list)
            return 0;
        else
            return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder viewHolder = null;
        final SortModel sortModel = list.get(position);
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.sort_list_item, null);
            viewHolder.tvLetter = (TextView) view.findViewById(R.id.catalog);
            viewHolder.tvName = (TextView) view.findViewById(R.id.sort_list_name);
            viewHolder.tvLabel = (TextView) view.findViewById(R.id.sort_list_label);
            viewHolder.ivHead = (ImageView) view.findViewById(R.id.sort_list_iv);
            viewHolder.sexIV = (ImageView) view.findViewById(R.id.sort_list_sex);
            viewHolder.addV = (ImageView) view.findViewById(R.id.sort_list_isIndustry);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        //根据position获取分类的首字母的Char ascii值
        int section = getSectionForPosition(position);

        //如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
        if (position == getPositionForSection(section)) {
            viewHolder.tvLetter.setVisibility(View.VISIBLE);
            viewHolder.tvLetter.setText(sortModel.getSortLetters());
        } else {
            viewHolder.tvLetter.setVisibility(View.GONE);
        }
        viewHolder.tvName.setText(this.list.get(position).getName());
        viewHolder.tvLabel.setText(list.get(position).getLabel() == null ? "" : list.get(position).getLabel());
        if (TextUtils.isEmpty(list.get(position).getUrl())) {
            viewHolder.ivHead.setImageResource(R.drawable.default_head_icon);
        } else {
            PictureDownloadUtils.displayHeadImage(mContext, viewHolder.ivHead, list.get(position).getUrl());
        }
        Utils.showSex(mContext, sortModel.getSex(), viewHolder.sexIV);
        if (sortModel.isIndustryAuthority()) {
            viewHolder.addV.setVisibility(View.VISIBLE);
        } else {
            viewHolder.addV.setVisibility(View.GONE);
        }

        return view;
    }

    static class ViewHolder {
        TextView tvLetter;
        TextView tvName;
        TextView tvLabel;
        ImageView ivHead;
        ImageView sexIV;
        ImageView addV;
    }

    /**
     * 根据ListView的当前位置获取分类的首字母的Char ascii值
     */
    public int getSectionForPosition(int position) {
        return list.get(position).getSortLetters().charAt(0);
    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     */
    public int getPositionForSection(int section) {
        for (int i = 0; i < getCount(); i++) {
            String sortStr = list.get(i).getSortLetters();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }
        return -1;
    }

}
